<?php
/**
 * Rest基础分析
 *
 * Copyright (c) 2005 - 2009  Softwe Pty Ltd. All rights reserved.
 * Support  : i9yu.com
 * Group    : 9yu.com Front Dev Group.
 * Author   : Lon&无喱头
 * Email    : phpsoho@tom.com
 * Encoding : UTF-8
 * This software is the proprietary information of phpsoho.com
 * $Id: Restbase.class.php Wed Dec 29 09:08:30 GMT 2010 by Lon&无喱头 $
*/

class RestBase{
	public $file,$key,$method,$type;
	public function __construct(){
		$this->get = array_keys($_GET);
		$this->getMethod();
	}
	public function getMethod(){
        if(isset($_SERVER["REQUEST_METHOD"])){
        	$method = $_SERVER["REQUEST_METHOD"];
        }else{
        	$method = "GET";
        }
        if ($method == "PUT") {
        	$method = "POST";
        }
        if ($method == "DELETE") {
        	$method = "GET";
        }
        $this->method = $method;
	}
	public function explode(){
		$str = explode("/",$this->get[0]);
		$this->file = $str[0];
		$this->key = $str[1];
		$str2 = explode("_",$str[2]);
		if (count($str2)>1) {
			$this->action = $str2[0];
			$this->__chkAction();
			if ($this->action=="update") {
				$this->method = "POST";
			}
			if ($this->action == "delete") {
				$this->method = "GET";
			}
			$this->type = $str2[1];
		}else{
			$this->type = $str[2];
		};unset($this->get);
		$this->__chkType();
	}
	private function __chkAction(){
		if (!in_array($this->action,array("update","delete"))) {
			die
			(
				"<h1><font color=red>Error:</font></h1>".
				"<li>Operation failure.</li> ".
				"<li>Designated motion commands does not exist or program accident, please contact the system administrator</li>"
			);
		}
	}
	private function __chkType(){
		if (!in_array($this->type,array("xml","json"))) {
			die
			(
				"<h1><font color=red>Error:</font></h1>".
				"<li>Operation failure.</li> ".
				"<li>Designated motion commands does not exist or program accident, please contact the system administrator</li>"
			);
		}
	}
}
?>